package com.ttai;


import com.ttai.untils.SRPClient;
import com.ttai.untils.SRPUtil;

import org.junit.Test;

import java.util.List;

/**
 * @program: taisecurity
 * @description: 测试SRP
 * @author: Jingxi Zhu
 * @create: 2018-11-19 13:45
 * @Copyright © 2018 Jingxi Zhu. All rights reserved
 **/
public class SRPtest {

    @Test
    public void srpTest(){
        String user = "15347835178";
        String password = "taisecrity:tai:t626123:7:taisecrity";
//        String password = CommonUtil.getSrpKey();
        List<String> st = SRPUtil.spiString(password,":");
        String[] pwd = new String[st.size()];
        for (int i=0;i<st.size();i++){
            pwd[i] = SRPUtil.tob64(st.get(i).getBytes());
        }
////        String[] pwd = {SRPUtil.tob64("taisecrity".getBytes()),SRPUtil.tob64("tai".getBytes()),SRPUtil.tob64("akuhgaku".getBytes())
////                ,SRPUtil.tob64("7".getBytes()),SRPUtil.tob64(("taisecrity").getBytes())};
//        System.out.print("数据库中的密码是：");
//        for(String pwdpr:pwd){
//            System.out.print(pwdpr);
//        }
//        System.out.println();
////        String[] pwd = {"1a","2b","3c","4d","5e"};
//        SRPServer serv = new SRPServer(user,pwd);

        //服务器生成n,g,s
        System.out.println("服务器生成n,g,s");
        String n = "t";
        String g = "1qOMbpPMDoQNHv";
        String s = "T65f";

        System.out.println("n (to client): " + n);
        System.out.println("g (to client): " + g);
        System.out.println("s (to client): " + s);
        System.out.println();

        //客户端接收n,g,s
        System.out.println("客户端接收n,g,s");
        SRPClient cli = new SRPClient(user, SRPUtil.fromb64(n),SRPUtil.fromb64(g),SRPUtil.fromb64(s));
//        SRPClient cli = new SRPClient(user,serv.modulus(),serv.generator(),serv.salt());

        //客户端生成A
        System.out.println("客户端生成A");
        byte[] cex = cli.generateExponential();
        String A = SRPUtil.tob64(cex);
        System.out.println("A (to server): " + A);

        //服务器生成B
        System.out.println("服务器拿到A后生成B");
//        byte[] ex = serv.generateExponential();
//        String B = SRPUtil.tob64(ex);
        String B = "1qOMbpPMDoQNHa";
        System.out.println("B (to client): " + B);

        //服务器接收A
        System.out.println("服务器接收A");
//        byte[] serverkey = serv.getSessionKey(SRPUtil.fromb64(A));
//        String Skey = SRPUtil.tohex(serverkey);
        String Skey = "DADA3939A3A3EEEE5E5E6B6B4B4B0D0D32325555BFBFEFEF9595606018189090AFAFD8D807070909";
        System.out.println("Session key from server: " + Skey);

        //客户端接收B
        System.out.println("客户端接收B");
        cli.inputPassword(pwd[2]);
//        cli.inputPassword(pwd[0]+pwd[1]+pwd[2]+pwd[3]+pwd[4]);
        byte[] key = cli.getSessionKey(SRPUtil.fromb64(B));
        String Ckey = SRPUtil.tohex(key);
        System.out.println("Session key form client: " + Ckey);

        //服务器接收客户端的Response
        System.out.println("服务器接收客户端的Response");
        String resp = SRPUtil.tohex(cli.response());
        System.out.println("Response (to server): " + SRPUtil.tohex(cli.response()));
        if(cli.verify(SRPUtil.fromhex(resp)))
            System.out.println("Authentication successful.");
        else
            System.out.println("Authentication failed.");
    }

}
